package com.app.pojo;

import com.app.utils.Judges;

public class MyPath {
    public String diskId,detailPath;
    /**
     *
     * @param sourcePath path 以“/”开头则访问自己的网盘，以userId开头则访问对应网盘，path不允许有./存在
     */
    public MyPath(String sourcePath){
        sourcePath=sourcePath.replace("/+|\\\\+","/");
        if (sourcePath.startsWith("/")) detailPath=sourcePath;
        else {
            int i = sourcePath.indexOf('/');
            if (i==-1){
                diskId=sourcePath;
                detailPath="/";
            }else {
                diskId = sourcePath.substring(0, i);
                detailPath = sourcePath.substring(i);
            }
        }
    }

    /**
     *
     * @param diskId 只能是数字
     * @param detailPath 必须以 / 开头
     */
    public MyPath(String diskId, String detailPath) {
        this.diskId = diskId;
        this.detailPath = detailPath;
    }

    public boolean isAvailable(){
        return Judges.judgePath(getSourcePath());
    }

    public void setDiskId(String diskId) {
        this.diskId = diskId;
    }

    public String getSourcePath(){
        String sourcePath=((diskId==null?"":diskId)+detailPath).replaceAll("/+|\\\\+","/");
        if ("/".equals(sourcePath)) return sourcePath;
        if (sourcePath.endsWith("/")) sourcePath=sourcePath.substring(0,sourcePath.length()-1);
        return sourcePath;

    }
    @Override
    public String toString() {
        return getSourcePath();
    }

    public MyPath getParentPath() {
        int id=detailPath.lastIndexOf("/");
        return new MyPath(diskId,detailPath.substring(0,id));
    }
    public String getDirname() {
        String sourcePath =getSourcePath();
        int id=sourcePath.lastIndexOf("/");
        return id>0? sourcePath.substring(sourcePath.lastIndexOf("/")+1):sourcePath;
    }

    public MyFile toMyFile() {
        return new MyFile(getSourcePath());
    }
}
